﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using MContracts.ViewModel;
using Microsoft.Win32;
using CommonBase;

namespace MContracts.View
{
    /// <summary>
    /// Interaction logic for DocumentImageView.xaml
    /// </summary>
    public partial class DocumentImageView : UserControl
    {
        public DocumentImageView()
        {
            InitializeComponent();
        }

        void RequestFileNameHandler(object sender, EventParameterArgs<string> e)
        {
            var dlg = new OpenFileDialog {Title = "Выберите файл для закачки"};
            if (dlg.ShowDialog().GetValueOrDefault())
                e.Parameter = dlg.FileName;
        }
        void RequestDownloadFileNameHandler(object sender, EventParameterArgs<string> e)
        {
            var dlg = new SaveFileDialog
                {
                    Title = "Укажите путь для загрузки файла",
                    FileName = e.Parameter,
                    InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
                };
            if (dlg.ShowDialog().GetValueOrDefault())
                e.Parameter = dlg.FileName;
            else
                e.Parameter = string.Empty;
        }

        DocumentImageViewModel ViewModel
        {
            get { return DataContext as DocumentImageViewModel; }
        }

       
        private void DataContextChangedHandler(object sender, DependencyPropertyChangedEventArgs e)
        {
            var ctxOld = e.OldValue as DocumentImageViewModel;
            var ctxNew = e.NewValue as DocumentImageViewModel;
           
            ctxOld.Do(x => x.RequestDownloadFileName -= RequestDownloadFileNameHandler);
            ctxOld.Do(x => x.RequestUploadFileName -= RequestFileNameHandler);
            ctxNew.Do(x => x.RequestDownloadFileName += RequestDownloadFileNameHandler);
            ctxNew.Do(x => x.RequestUploadFileName += RequestFileNameHandler);
           

        }
    }
}
